package servlet;

import dao.StudentD;
import vo.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.regex.Pattern;

//查询出一个页面的学生信息添加到session里传递到jsp页面显示
@WebServlet("/one_page_student")
public class one_page_student extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");

        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();

        String key = request.getParameter("key"); // 查找功能

        if (key == null || key.equals("")) {
            int currentIndex, count, size = 10;
            String index = request.getParameter("index");
            if (index == null) index = "1";
            currentIndex = Integer.parseInt(index);
            try {
                StudentD sdao = new StudentD();
                // 向数据库获取当前页面的学生信息
                ArrayList<Student> students = sdao.getOnePage(currentIndex, size);
                count = sdao.getStudentCount();
                // 计算总页数
                int sumIndex = count % size == 0 ? count / size : count / size + 1;
                // 设置session内容
                session.setAttribute("onePageStudent", students);
                session.setAttribute("currentIndex", currentIndex);
                session.setAttribute("sumIndex", sumIndex);
                response.sendRedirect("teacher/main.jsp");
            } catch (Exception e) {
                out.print(e);
            }
        } else { // 查找功能（按照学号或姓名）
            StudentD studentD = new StudentD();
            String pattern = "^\\d+"; // 匹配一个或多个连续的数字（0-9）
            boolean isMatch = Pattern.matches(pattern, key);
            if (isMatch) {
                try {
                    Student student = studentD.findWithId(key);
                    ArrayList<Student> students = new ArrayList<>();
                    students.add(student);
                    session.setAttribute("onePageStudent", students);
                    session.setAttribute("currentIndex", 1);
                    session.setAttribute("sumIndex", 1);
                    response.sendRedirect("teacher/main.jsp");
                } catch (Exception e) {
                    out.print(e);
                }
            } else {
                try {
                    ArrayList<Student> students = studentD.findWithName(key);
                    session.setAttribute("onePageStudent", students);
                    session.setAttribute("currentIndex", 1);
                    session.setAttribute("sumIndex", 1);
                    response.sendRedirect("teacher/main.jsp");
                } catch (Exception e) {
                    out.print(e);
                }
            }
        }
    }
}
